//
//  AllOrderMessageVC.m
//  QoilPublic
//
//  Created by SunChao on 2017/12/9.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "AllOrderMessageVC.h"
#import "CustomTableView.h"
#import "OrderListModel.h"
#import "DetailMessageVC.h"
#import "MessageCell.h"
@interface AllOrderMessageVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,assign)int index;

@end

@implementation AllOrderMessageVC
- (id)init{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"消息通知";
    _dataArr = [NSMutableArray array];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.clipsToBounds = YES;
    //初始化对象
    //NSArray *statusArr = @[@(6),@(2),@(3),@(4)];
    [self.view addSubview:self.tbView];
    
    _index = 0;
    self.tbView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    _tbView.mj_header = [QViewUtility showHeadRefresh:^{
        [_tbView.mj_footer resetNoMoreData];
        [self requestData];
    }];
    _tbView.mj_footer = [QViewUtility showFootRefresh:^{
        [self requestMoreData];
    }];
    [self requestData];
    [self.tbView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:nil] forCellReuseIdentifier:@"MessageCell"];
//    [self.tbView registerClass:[MessageCell class] forCellReuseIdentifier:@"CustomCell"];
    
   

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    //半透明
    self.navigationController.navigationBar.translucent = NO;
    
}

- (void)requestData{
    _index = 0;
    NSString *star = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"getMsgOrderList"];
    
    NSDictionary *para = [QParamService gainOrderInfoWithStart:star pageSize:@"5"];
   
    [QViewUtility showMBHudToView:self.view hideBg:YES];
    [QViewUtility disMissNoRecordInVC:self];
    [QViewUtility disMissNoNetWorkInVC:self];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
         [_tbView.mj_header endRefreshing];
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
                [QViewUtility showNoNetWorkInView:_tbView];
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            
            NSLog(@"------%@",basModel.result);
            [_dataArr removeAllObjects];
                  NSArray *array  = [OrderListModel mj_objectArrayWithKeyValuesArray:[basModel.items objectForKey:@"list"]];
            [_dataArr addObjectsFromArray:array];
            if(_dataArr.count == 0){
                
                [QViewUtility showNoRecordViewInView:self.view Title:@"没有任何消息哦~"];
                
            }else{
               
                [self.tbView reloadData];
            }
            
        }else{
            
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            
        }
        
    }];
}
//加载更多
- (void)requestMoreData{
    _index++;
    NSString *star = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"getMsgOrderList"];
    
    NSDictionary *para = [QParamService gainOrderInfoWithStart:star pageSize:@"5"];
    
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_footer endRefreshing];
      
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            
            NSLog(@"------%@",basModel.result);
            NSArray *array  = [OrderListModel mj_objectArrayWithKeyValuesArray:[basModel.items objectForKey:@"list"]];
            [_dataArr addObjectsFromArray:array];
            
            if(array.count < 10){
                [_tbView.mj_footer noticeNoMoreData];
                
            }
                
                [self.tbView reloadData];
            
        }else{
            
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            
        };
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 110;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell"];
   
    OrderListModel *model = [_dataArr objectAtIndex:indexPath.row];
    
    [cell setCellWithData:model];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DetailMessageVC *vc = [[DetailMessageVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
     OrderListModel *model = [_dataArr objectAtIndex:indexPath.row];
    vc.model = model;
    
}
@end
